﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using QLNHK.BLL;

namespace QLNHK
{
    public partial class frmCauHinh : Form
    {
        public frmCauHinh()
        {
            InitializeComponent();
        }

        private void frmCauHinh_Load(object sender, EventArgs e)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                string[] arrayStr = config.ConnectionStrings.ConnectionStrings["MSSQL"].ConnectionString.Split(';');
                txtServer.Text = arrayStr[0].Split('=')[1];
                txtDatabase.Text = arrayStr[1].Split('=')[1];
                txtUser.Text = arrayStr[2].Split('=')[1];
                txtPass.Text = arrayStr[3].Split('=')[1];

                txtBackupPath.Text = config.AppSettings.Settings["BACKUP_PATH"].Value.ToString();

            }
            catch
            { 
            
            }
            
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                string strConn = "Database =" + txtDatabase.Text + ";" + "Server=" + txtServer.Text + ";" + "UID=" + txtUser.Text + ";" + "PWD=" + txtPass.Text;
                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();
                MessageBox.Show("Kết nối thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kết nối không thành công.\nKiểm tra lại các thông số kết nối.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               
            }
        }

       
        private void btnLuu_Click(object sender, EventArgs e)
        {
            string strConn = "Database =" + txtDatabase.Text + ";" + "Server=" + txtServer.Text + ";" + "UID=" + txtUser.Text + ";" + "PWD=" + txtPass.Text;
            // TODO: Protect the Connection Strings

            Configuration config = null;
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConnectionStringSettings setting = new ConnectionStringSettings("MSSQL", strConn, "System.Data.SqlClient");
            config.ConnectionStrings.ConnectionStrings[1] = setting;
            ConfigurationSection section = config.ConnectionStrings;
            config.AppSettings.Settings["BACKUP_PATH"].Value = txtBackupPath.Text;

            if ((section.SectionInformation.IsProtected == false) &&
                (section.ElementInformation.IsLocked == false))
            {

                // Save the encrypted section.
                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full);
            }
            config.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("MSSQL");
            MessageBox.Show("Lưu cấu hình thành công.");
            this.Close();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSaoLuuPhucHoi_Click(object sender, EventArgs e)
        {
            FrmBackupRestore f = new FrmBackupRestore();
            f.ShowDialog();
        }
    }
}
